package zl.io;
import java.io.File;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;

public class IODemo02
{
	public static void main(String[] args)
	{
		File file = new File("C:" + File.separator + "Test.txt");
		OutputStream out = null;
		InputStream in = null;
		try
		{
			if(file.exists())
			{
				in = new FileInputStream(file);
				byte[] b = new byte[(int)file.length()];
				for(int i = 0; i < b.length; i++)
				{
					b[i] = (byte)in.read();
				}
				System.out.println(new String(b));
			}
			else
			{
				out = new FileOutputStream(file);
				String str = "Hello World!!!";
				byte[] b = str.getBytes();
				for(int i = 0; i < b.length; i++)
				{
					out.write(b[i]);
				}
			}
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if(in != null)
				{
					in.close();
				}
				else if(out != null)
				{
					out.close();
				}
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
}